package cn.com.jinhao.provider.business.service.impl;

import cn.com.jinhao.hotline.commons.base.constant.SystemConstant;
import cn.com.jinhao.provider.business.entity.SysEvcu;
import cn.com.jinhao.provider.business.entity.Wkfl;
import cn.com.jinhao.provider.business.mapper.SysEvcuMapper;
import cn.com.jinhao.provider.business.mapper.WkflMapper;
import cn.com.jinhao.provider.business.service.WorkflowService;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WorkflowServiceImpl extends CommonService implements WorkflowService {
    private final Log log = LogFactory.getLog(WorkflowServiceImpl.class);
    WkflMapper wkflMapper;
    SysEvcuMapper sysEvcuMapper;
    @Autowired
    public WorkflowServiceImpl(WkflMapper wkflMapper, SysEvcuMapper sysEvcuMapper){
        this.wkflMapper = wkflMapper;
        this.sysEvcuMapper = sysEvcuMapper;
    }


    @Override
    public boolean insert(Wkfl wkfl) {
        SysEvcu evcu = new SysEvcu();
        evcu.setPaid(wkfl.getEvcuPaid());
        //根据节点获取下一个节点的任务
        List<SysEvcu> evcu_List = sysEvcuMapper.select(evcu);
        boolean bool = false;
        for (SysEvcu sysEvcu :evcu_List) {
            //判断是否包含下一个节点
            if (sysEvcu.getCode().equals(wkfl.getEvcuCode())) {
                bool = true;
                break;
            }
        }
        if(bool){
            wkfl.setCode(getCode(SystemConstant.CodeType.COMPLEX));
            int add_count = wkflMapper.insertSelective(wkfl);
            if (add_count > 0 ) {
                log.info("新增任务流程成功");
                return true;
            }
        }
        return false;
    }

    @Override
    public void delete(String id) {
        wkflMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> select(Wkfl wkfl) {
        List<Wkfl> wkfl_List = wkflMapper.select(wkfl);
        Map<String,Object> wkfl_Map = new HashMap<>();
        wkfl_Map.put("wkfl_Map",wkfl_List);
        return wkfl_Map;
    }

    @Override
    public void update(Wkfl wkfl) {
        wkflMapper.updateByPrimaryKeySelective(wkfl);
    }

    @Override
    public String queryStatu(String btEvifCode,String btEvasCode) {
        Wkfl wkfl = new Wkfl();
        if(btEvifCode.isEmpty()){
            return "事件编号不能为空";
        }
        wkfl.setBtEvifCode(btEvifCode);
        wkfl.setBtEvasCode(btEvasCode);
        return wkflMapper.queryStatu(wkfl);
    }
}
